<%@ page contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 


<script>
var addUser = false;

function _submit(){
	if(addUser){
		showMessage("請檢查用戶名稱",2);
		return;
	}
	
	var _require = false;
	var _username = trim($("#username").val());
	var _password = $("#password").val();
	var _fullname = trim($("#fullname").val());
	var _email    = trim($("#email").val());
	
	_require = (_username.length == 0);
	_require = (_password.length == 0);
	_require = (_fullname.length == 0);
	_require = (_email.length == 0);
	
	if(_require){
		showMessage("欄位尚未填寫完畢",2);
		return;
	}
	
	
	$.post("${appPath}/admin/user/add.htm",$('#UsersForm').serialize(),function(data){
		if(data){
			var f = document.getElementById('UsersForm');
			f.reset();
			showMessage("用戶新增成功",4);
		}else{
			showMessage("用戶新增失敗",2);
		}
	});
}


function isUsernameExist(username){
	var loading = "<img src='${appPath }/resource/image/ajax-loader.gif' />";	
	$("#isExistUsersSpan").html(loading).show();
	$.post("${appPath}/admin/user/exist.htm",{username:username},function(data){
		var icon = data ? "1317492688_attention1.gif" : "1317488036_ok.gif";	
		var img = "<img src='${appPath }/resource/image/" + icon + "' />";		
		$("#isExistUsersSpan").hide().html(img).fadeIn('slow');
		addUser = data;
	});
}


</script>


<div id="page">
	<div id="page-bgtop">
		<div id="page-bgbtm">
			<div id="content">
				<div class="post">
				<!--  
					<h2 class="title inner">用戶新增</h2>
					
					<div style="clear: both;">&nbsp;</div>
				-->
					<div class="entry">
						
						<form:form method="post" modelAttribute="UsersForm" action="${appPath}/admin/users/add" >
							<table style="width:450px;border:1px solid #D2D9E7;">
								<tr class="trContent">
									<td style="width:15%;text-align: right;padding-right:15px;">帳號</td> 
									<td>
										<form:input path="username" cssClass="bar_1" cssStyle="width:200px;" onblur="isUsernameExist(this.value);"/>
										<span id="isExistUsersSpan" style="padding-left:5px;display:none"></span>										
									</td> 
								</tr>
								<tr class="trContent">
									<td style="text-align: right;padding-right:15px;">密碼</td> 
									<td><form:password path="password" cssClass="bar_1" cssStyle="width:200px;"/></td> 
								</tr>
								<tr class="trContent">
									<td style="text-align: right;padding-right:15px;">全名</td> 
									<td><form:input path="fullname" cssClass="bar_1" cssStyle="width:200px;"/></td> 
								</tr>
								<tr class="trContent">
									<td style="text-align: right;padding-right:15px;">E-Mail</td> 
									<td><form:input path="email" cssClass="bar_1" cssStyle="width:300px;"/></td> 
								</tr>
								<tr class="trContent">
									<td style="text-align: right;padding-right:15px;">角色</td> 
									<td>
										<form:checkboxes items="${ITEMS_ROLE}" itemLabel="description" itemValue="name" path="roles" />
									</td> 
								</tr> 
								<tr style="background-color: #F2F2F2;">
									<td colspan="2" style="text-align: center;height:80px;">
										<span class="button_2" style="width:60px;">清　除</span>								
										<span class="button" style="width:60px;" onclick="_submit();">確　認</span> 									
									</td> 
								</tr>
							</table>
						
						</form:form>
						
					
					</div>
				</div>
				<div style="clear: both;">&nbsp;</div>
			</div>
			<!-- end #content -->
			
			<%@ include file="/WEB-INF/jsp/common/submenu01.jsp" %>			
			<div style="clear: both;">&nbsp;</div>
		</div>
	</div>
</div>
<!-- end #page -->